package principle.lawOfDemeter.perfection;

import principle.lawOfDemeter.origin.Student;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author： pang-yun
 * @date： 2021-08-25 14:49
 * 功能目的 ：用于 本班学生的 数据处理 尽量放在 teacher中 尽量不与其他类产生关联！
 */

@Getter
public class Teacher {
    private String name;
    private String clazz;
    private static List<Student> studentList;

    public Teacher(String name, String clazz) {
        this.name = name;
        this.clazz = clazz;
    }

    static {
        studentList= new ArrayList<Student>();
        studentList.add(new Student("pang",10,555));
        studentList.add(new Student("chao",11,556));
        studentList.add(new Student("jie",12,557));
        studentList.add(new Student("god",1,5555));
    }

    public static List<Student> getStudentList(){
        return studentList;
    }

    //总分
    public double scoreTotal(){
        List<Student> studentList = getStudentList();
        Optional<Double> reduce = studentList.stream().map(a -> a.getGrade()).reduce(Double::sum);
        return  reduce.get();
    }

    //平均分
    public double averageScore(){
        List<Student> studentList = getStudentList();
        Double collect = studentList.stream().collect(Collectors.averagingDouble(Student::getGrade));
        return collect;
    }

    //学生人数
    public int countStudent(){
        return getStudentList().size();
    }
}
